package com.shimao.celnet.payment.util;

import com.shimao.celnet.payment.model.Bill;
import de.siegmar.fastcsv.reader.CsvContainer;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRow;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: MattChen
 * @Description: csv文件解析工具包
 * @Date: 16:04 2019/5/21
 * @ModifiedBy:
 */
public class CsvUtils {

    /*
     * 功能描述: 解析通联账单
     * @Author: Matt
     * @Date: 2019/5/22 10:32
     */
    public static List<Bill> csvReadOperation(String path) throws IOException {
        File file = new File(path);
        CsvReader csvReader = new CsvReader();
        csvReader.setContainsHeader(true);

        CsvContainer csv = csvReader.read(file, Charset.forName("GBK"));
        List<Bill> bills = new ArrayList<>();
        for (CsvRow row : csv.getRows()) {
            Bill bill = new Bill();
            bill.setAppId(row.getField(18));
            bill.setBankCode(row.getField(10));
            bill.setBankName(row.getField(11));
            bill.setCardNum(row.getField(8));
            bill.setCardType(row.getField(9));
            bill.setCustId(row.getField(0));
            String money = row.getField(12);
            bill.setMoney(doubleStringToInteger(money));
            String originalMoney = row.getField(19);
            bill.setOriginalMoney(doubleStringToInteger(originalMoney));
            bill.setPaymentId(row.getField(16));
            String payMoney = row.getField(20);
            bill.setPayMoney(doubleStringToInteger(payMoney));
            bill.setPhoneNum(row.getField(17));
            bill.setProductType(row.getField(21));
            bill.setReferenceNum(row.getField(6));
            String serviceCharge = row.getField(13);
            bill.setServiceCharge(doubleStringToInteger(serviceCharge));
            bill.setShopName(row.getField(1));
            bill.setTerminalId(row.getField(2));
            bill.setTransactionDate(LocalDate.parse(row.getField(14)));
            bill.setTransactionNum(row.getField(5));
            bill.setTransactionTime(LocalTime.parse(row.getField(3)));
            bill.setTransactionType(row.getField(4));
            bill.setTrxId(row.getField(15));
            bill.setVoucherNum(row.getField(7));
            bills.add(bill);
        }
        return bills;
    }
    public static Integer doubleStringToInteger(String para){
        if("".equals(para)){
            return 0;
        }else{
            Double money = Double.valueOf(para)*100;
            int b=money.intValue();
            return Integer.valueOf(b);
        }
    }
}
